package com.aruba.girlanime.delegate;

import android.animation.ObjectAnimator;
import android.graphics.PointF;
import android.os.Handler;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.daimajia.androidanimations.library.BaseViewAnimator;
import com.daimajia.androidanimations.library.YoYo;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 烟花动画
 * Created by aruba on 2018/5/11.
 */

public class HanabiAnimeDelegate implements IAnimeDelegate {
    private View[] views;
    private List<PointF> pointFs = new LinkedList<>();
    private float width, height;
    private boolean isRunning;

    @Override
    public void startAnime() {
        Collections.shuffle(pointFs);

        
        if(!isRunning){
            isRunning = true;

            for (int i = 0; i < pointFs.size(); i++) {
                final PointF pointF = pointFs.get(i);

                long de = (long) (2000 * Math.random());

                final int finalI = i;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        YoYo.with(new HanabiAnimator(pointF.x - views[finalI].getWidth() / 2, pointF.y - views[finalI].getHeight() / 2))
                                .repeat(YoYo.INFINITE)
                                .interpolate(new LinearInterpolator())
                                .duration((long) (2500 * (Math.random() + 1)))
                                .playOn(views[finalI]);

                        views[finalI].setVisibility(View.VISIBLE);
                    }
                }, de);
            }
        }
    }

    @Override
    public void init(View... args) {
        args[0].addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (width == 0) {
                    width = v.getWidth();
                    height = v.getHeight() / 2;

                    //初始化位置
                    pointFs.add(new PointF(width / 4, height / 4));
                    pointFs.add(new PointF(width / 2, height / 6));
                    pointFs.add(new PointF(width * 2 / 3, height / 3));
                    pointFs.add(new PointF(width / 5, height * 4 / 5));
                    pointFs.add(new PointF(width * 0.47f, height * 3 / 5));
                    pointFs.add(new PointF(width * 0.75f, height * 4 / 5));
                }
            }
        });
        views = new View[args.length - 1];
        System.arraycopy(args, 1, views, 0, views.length);
    }

    @Override
    public void stopAnime() {

    }

    @Override
    public void reset() {

    }

    public class HanabiAnimator extends BaseViewAnimator {
        private float x;
        private float y;

        public HanabiAnimator(float x, float y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public void prepare(final View target) {
            target.setX(x);
            target.setY(y);

            getAnimatorAgent().playTogether(
                    ObjectAnimator.ofFloat(target, "scaleY", .2f, 2f),
                    ObjectAnimator.ofFloat(target, "scaleX", .2f, 2f),
                    ObjectAnimator.ofFloat(target, "alpha", 1, 0),
                    ObjectAnimator.ofFloat(target, "translationX", x, x),
                    ObjectAnimator.ofFloat(target, "translationY", y, y)
            );
        }
    }
}
